package com.gaohe.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.DishMapper;
import com.gaohe.pojo.Category;
import com.gaohe.pojo.Dish;
import com.gaohe.pojo.DishDto;
import com.gaohe.pojo.DishFlavor;
import com.gaohe.service.CategoryService;
import com.gaohe.service.DishFlavorService;
import com.gaohe.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Override
    @Transactional
    public void saveDishDto(DishDto dishDto) {
        //插入dish表数据
        this.save(dishDto);
        //将flavor表的id进行赋值
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> dishFlavors = new ArrayList<DishFlavor>();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavors.add(flavor);
        }
        //插入flavor表的数据
        dishFlavorService.saveBatch(dishFlavors);
    }

    @Override
    public Page<DishDto> DISH_DTO_PAGE(Integer page, Integer pageSize, String name) {
        //先查询dishPage的内容
        Page<Dish> dishPage = new Page(page, pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
        lqw.like(name != null, Dish::getName, name);
        this.page(dishPage, lqw);
        //建立dishDtoPage
        Page dishDtoPage = new Page(page, pageSize);
        //将dishPage的内容复制到dishDtoPage中，其中records字段加上categoryName之后再给到dishDtoPage
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
        //将dishRecords加上categoryName
        List<Dish> dishRecords = dishPage.getRecords();
        List<DishDto> dishDtoList = new ArrayList<DishDto>();
        for (Dish dishRecord : dishRecords) {
            DishDto dishDto = new DishDto();
            Long categoryId = dishRecord.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            BeanUtils.copyProperties(dishRecord, dishDto);
            dishDto.setCategoryName(categoryName);
            dishDtoList.add(dishDto);
        }
        dishDtoPage.setRecords(dishDtoList);

        //返回dishDtoPage
        return dishDtoPage;
    }

    //修改菜品数据，回显
    @Override
    @Transactional
    public DishDto updateDishAndFlavor1(Long id) {
        DishDto dishDto = new DishDto();
        //根据菜品id来查询dish表的内容
        Dish dish = this.getById(id);
        //根据菜品信息查询category表的categoryName
        Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        //根据菜品信息来查询flavor表的数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw);
        //封装到dishDto向前端返回
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavors);
        dishDto.setCategoryName(category.getName());
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishAndFlavor2(DishDto dishDto) {
        //先删除，再新增
        //dish表直接修改
        this.updateById(dishDto);
        //flavor表先删后新增
        //删除
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper();
        //flavor表根据dishId来删除多个数据
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        //新增
        List<DishFlavor> flavors = dishDto.getFlavors();
        //向flavor集合添加dishId
        List<DishFlavor> dishFlavors = flavors.stream().map(flavor -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavors);
    }

    @Override
    @Transactional
    public void stop(Integer status, String ids) {
        //处理接收的ids
        String[] split = ids.split(",");
        //转换为集合
        List<String> idList = Arrays.asList(split);
        //转换为long类型的集合
        List<Long> collect = idList.stream().map(list -> {
            long parseLong = Long.parseLong(list);
            return parseLong;
        }).collect(Collectors.toList());
        //查出来菜品表数据
        List<Dish> dishes = this.listByIds(collect);
        //遍历修改菜品的状态码
        for (Dish dish : dishes) {
            dish.setStatus(status);
        }
        this.updateBatchById(dishes);
    }

    @Override
    @Transactional
    public void deleteDishAndFlavor(String ids) {
        //查询并删除菜品表的数据
        //处理接收的ids
        String[] split = ids.split(",");
        //转换为long类型的集合
        List<String> list = Arrays.asList(split);
        this.removeBatchByIds(list);
        //falvor表来根据菜品表的dish_id来删除数据
        List<Dish> dishes = this.listByIds(list);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper();
        for (String s : list) {
            lqw.eq(DishFlavor::getDishId, s);
            dishFlavorService.remove(lqw);
        }


    }

    @Override
    public List<DishDto> DISH_DTO_LIST(Dish dish) {
        Long categoryId = dish.getCategoryId();
        //根据categoryId来查询菜品信息
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
        lqw.eq(categoryId != null, Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1)
                .orderByDesc(Dish::getUpdateTime);
        List<Dish> list = this.list(lqw);
        List<DishDto> dishDtos = list.stream().map(dish1 -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1, dishDto);
            LambdaQueryWrapper<DishFlavor> lqw1 = new LambdaQueryWrapper();
            lqw1.eq(DishFlavor::getDishId, dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(lqw1);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtos;
    }


}
